import 'package:canteen/dio/pR_dio.dart';
import 'package:canteen/edit_c_v.dart';
import 'package:canteen/history_c_v.dart';
import 'package:canteen/method/showDialog.dart';
import 'package:canteen/userDetail.dart';
import 'package:flutter/material.dart';
import 'package:canteen/dio/configure_dio.dart';

class EditLinePage extends StatefulWidget {
  static String id = 'edit_line_page';

  @override
  State<EditLinePage> createState() => _EditLinePageState();
}

class _EditLinePageState extends State<EditLinePage> {
  final TextEditingController textEditingController = TextEditingController();
  String title = '';
  late String oldName;

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    final Map<String, dynamic>? args =
        ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;
    title = args?['title'] ?? '';
    if (title == '密码') {
      textEditingController.text = password;
    } else {
      textEditingController.text = name;
      oldName = name;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfffbfbfb),
      appBar: AppBar(
        backgroundColor: Color(0xffffffff).withOpacity(0.89),
        elevation: 0,
        centerTitle: true,
        title: Text(
          '${title}',
          style: TextStyle(color: Colors.black, fontSize: 16),
        ),
        leading: GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: Icon(
            Icons.arrow_back_rounded,
            color: Colors.black,
          ),
        ),
        actions: [
          Row(
            children: [
              GestureDetector(
                onTap: () {
                  if (title == '包房') {
                    updatePrivateRoom();
                  } else {
                    if (title == '密码') {
                      password = textEditingController.text;
                    } else {
                      name = textEditingController.text;
                    }
                    Navigator.pop(context);
                  }
                },
                child: Text(
                  '保存',
                  style: TextStyle(
                    color: Colors.redAccent,
                    fontSize: 16,
                  ),
                ),
              ),
              SizedBox(
                width: 20,
              )
            ],
          )
        ],
      ),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          children: [
            SizedBox(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text(
                  '${title}',
                  style: TextStyle(color: Colors.grey, fontSize: 14),
                ),
              ],
            ),
            TextField(
              maxLength: title == '品类'
                  ? 5
                  : title == '包房'
                      ? 8
                      : 15,
              controller: textEditingController,
              decoration: InputDecoration(
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey), // 默认状态颜色
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey), // 聚焦状态颜色
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  void updatePrivateRoom() async {
    try {
      await updatePR(
          privateRoom: {'oldName': oldName, 'name': textEditingController.text},
          dio: dio);
      privateRooms[privateRooms.indexWhere((element) => element == oldName)] =
          textEditingController.text;
      oldName = textEditingController.text;
      name = textEditingController.text;
      ShowDialog.showCorrectAndDetail(context, '上传成功');
    } on Exception catch (e) {
      ShowDialog.showErrorAndDetail(context, '上传失败');
    }
  }
}
